var boolDebug = false;
//boolDebug = true;	//	uncomment to debug
var boolAlertDebug = false;
var boolConsoleLogDebug = false;
if(boolDebug) {
	boolAlertDebug = true;	//	commenting this will cause alerts to stop displaying
	//boolConsoleLogDebug = true;
}

/*
	Perform a case-insensitive comparison of the values of the textboxes with the two passed strings, and return a boolean indicating whether or not they are equal to each other.
	@param a the ID of the first textbox
	@param b the ID of the second textbox
*/
function areFieldValuesEqualIgnoreCase(a, b) {
	return equalsIgnoreCase(document.getElementById(a).value, document.getElementById(b).value);
}

/*
	Return a boolean indicating whether or not an array contains a specified value.
	@param aNeedle the item to search for in the array.
	@param aHaystack the array to search for
*/
function arrayContains(aNeedle, aHaystack) {
	var intIndex = getArrayIndex(aNeedle, aHaystack);
	if(intIndex == -1) {
		return false;
	} else {
		return true;
	}
}

/*
	Perform a case-insensitive comparison of two passed strings and return a boolean indicating whether or not they are equal to each other.
	@param a the first string
	@param b the second string
*/
function equalsIgnoreCase(a, b) {
	return a.toLowerCase() == b.toLowerCase();
}

/*
	Pass a JQuery selector for an element to center it horizontally on the screen.  In order for this to occur successfully, all content must be loaded within the element prior to calling this function on it.  In order to 
	determine the horizontal position, the window's current width and scroll position, as well as the width of the target element, must be taken into consideration.  JQuery is required.
	@param aSelector the JQuery selector for the element to position
*/
function fCenterHorizontallyOnScreen(aSelector) {
	if(!(aSelector == undefined || aSelector == null || aSelector == "")) {
		var arrWindowDimensions = fGetWindowSize();
		var intWindowWidth = arrWindowDimensions[0];
		var intScrollPosition = fGetScrollPositionHorizontal();
		var intLeft = intScrollPosition + intWindowWidth / 2 - $(aSelector).outerWidth() / 2;
//alert("intWindowWidth = " + intWindowWidth + "\nintScrollPosition = " + intScrollPosition + "\n");
		$(aSelector).css("left", intLeft);
	}
}

/*
	Pass a JQuery selector for an element to center it vertically on the screen.  In order for this to occur successfully, all content must be loaded within the element prior to calling this function on it.  In order to 
	determine the vertical position, the window's current height and scroll position, as well as the height of the target element, must be taken into consideration.  JQuery is required.
	@param aSelector the JQuery selector for the element to position
*/
function fCenterVerticallyOnScreen(aSelector) {
	if(!(aSelector == undefined || aSelector == null || aSelector == "")) {
		var arrWindowDimensions = fGetWindowSize();
		var intWindowHeight = arrWindowDimensions[1];
		var intScrollPosition = fGetScrollPositionVertical();
		var intHeight = $(aSelector).outerHeight();
		var intTop;
		if(intWindowHeight > intHeight) {
			intTop = Math.round(intScrollPosition + intWindowHeight / 2 - intHeight / 2);
		} else {
			intTop = 20;
		}
		fDebug("intWindowHeight = " + intWindowHeight + "\nintScrollPosition = " + intScrollPosition + "\nintHeight = " + intHeight + "\nintTop = " + intTop + "\n");
		$(aSelector).css("top", intTop);
	}
}

/*
	Based on the global boolDebug, boolAlertDebug and boolConsoleLogDebug variables, display the passed debug information using the appropriate method.
	@param aString the debug string to display
*/
function fDebug(aString) {
	//alert("fDebug() called.\nboolAlertDebug = " + boolAlertDebug + "\nboolConsoleLogDebug = " + boolConsoleLogDebug + "\n");
	if(boolAlertDebug) {
		alert(aString);
	}
	if(boolConsoleLogDebug) {
		console.log(aString);
	}
}

//	Return the horizontal scroll position.
function fGetScrollPositionHorizontal() {
	var output = document.body.scrollLeft;
	if (output == 0)
	{
    if (window.pageXOffset)
      output = window.pageXOffset;
    else
      output = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
	}
	return output;
}

//	Return the vertical scroll position.
function fGetScrollPositionVertical() {
	var output = document.body.scrollTop;
	if (output == 0)
	{
    if (window.pageYOffset)
      output = window.pageYOffset;
    else
      output = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return output;
}

//	Return an array with two integers: {width, height}
function fGetWindowSize(intDefaultWidth, intDefaultHeight) {
	var width = intDefaultWidth ? intDefaultWidth : -1;
	var height = intDefaultHeight ? intDefaultHeight : -1;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    if (window.innerWidth > width)
	    width = window.innerWidth;
	  if (window.innerHeight > height)
	    height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    if (document.documentElement.clientWidth > width)
	    width = document.documentElement.clientWidth;
	  if (document.documentElement.clientHeight > height)
    height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    if (document.body.clientWidth > width)
	    width = document.body.clientWidth;
  	if (document.body.clientHeight > height)
    height = document.body.clientHeight;
  }
  var arrDimensions = new Array();
  arrDimensions[0] = width;
  arrDimensions[1] = height;
  return arrDimensions;
}

/*
	Return the numeric index of the target item within an array or, if it doesn't exist, return -1.
	@param aHaystack An array to search through
	@param aNeedle An item to search for within the array
*/
function getArrayIndex(aHaystack, aNeedle) {
	var output = -1;
	if(aHaystack != null && aHaystack != undefined) {
		if (!Array.prototype.indexOf) {	//	Necessary because IE<9 doesn't have an .indexOf() method of the Array class
		  Array.prototype.indexOf = function(elt /*, from*/)
		  {
			var len = this.length >>> 0;

			var from = Number(arguments[1]) || 0;
			from = (from < 0)
				 ? Math.ceil(from)
				 : Math.floor(from);
			if (from < 0)
			  from += len;

			for (; from < len; from++)
			{
			  if (from in this &&
				  this[from] === elt)
				return from;
			}
			return -1;
		  };
		}
		output = aHaystack.indexOf(aNeedle);
	}
	return output;
}

/******************************************
This deals with show * in the right color 
in asp:requiredfiledvalidator
******************************************/
function redGreyAsterix() {
  var boolDebug = false;
  //boolDebug = true;  //  Uncomment to debug
  
  if(boolDebug) {
    console.log("HERE");
  }
  $('.ast').each(function () {
    
      if ($(this).parent().is(':visible')) {
        if(boolDebug) {
          console.log("Visible");
        }
        $(this).removeClass('grey').addClass('red');
      } else {
        if(boolDebug) {
          console.log("Hidden");
        }
        $(this).removeClass('red').addClass('grey');
        $(this).parent().removeAttr("stytle").show();
      }
  });
}

